package com.ruoyi.partybuilding.controller;

import cn.hutool.json.JSONUtil;
import com.ruoyi.partybuilding.domain.MeetingInfo;
import com.ruoyi.partybuilding.domain.UserMemberDeptV;
import io.swagger.annotations.*;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

import lombok.var;
import com.ruoyi.common.core.domain.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.partybuilding.domain.MeetingReceipt;
//import com.ruoyi.partybuilding.service.MeetingReceiptService;
import com.ruoyi.partybuilding.common.*;
import reactor.util.function.Tuple3;
import reactor.util.function.Tuples;

/**meeting_receipt
 * 会议回执;(pb_meeting_receipt)表控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2024-12-5
 */
@Api(tags = "会议回执对象功能接口")
@RestController
@RequestMapping("/partybuilding/MeetingReceipt")
public class MeetingReceiptController extends EQueryController<MeetingReceipt>{

    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('partybuilding:MeetingReceipt:query')")
    @GetMapping(value = "/{id}")
    public R<MeetingReceipt> getInfo(@PathVariable("id") Long id)
    {
        return super.getInfo(id);
    }
    @ApiOperation("分页")
    @PreAuthorize("@ss.hasPermi('partybuilding:MeetingReceipt:list')")
    @GetMapping("query")
    public R<Page<MeetingReceipt>> query(MeetingReceipt whereObj, PageDomain pageRequest) {
        return super.queryPage(whereObj, pageRequest);
    }
    @ApiOperation("列表,对query的rouyi兼容包装")
    @PreAuthorize("@ss.hasPermi('partybuilding:MeetingReceipt:list')")
    @GetMapping("list")
    public TableDataInfoRows<MeetingReceipt> list(MeetingReceipt entity, PageDomain pageRequest) {
        return super.list(entity,pageRequest);
    }

    @ApiOperation(value = "会议签到统计",notes = "t1 总人数，t2签到人列表,t3未签到人员列表")
    @PreAuthorize("@ss.hasPermi(@pb.meetingInfo.signManage)")
    @GetMapping("meetingSignStat")
    public R<Tuple3<Integer, List<MeetingReceipt>, List<MeetingReceipt>>> meetingSignStat(Long meetingId) {
        var re = queryHelper.queryable(MeetingReceipt.class)
                .where(p -> p.eq(MeetingReceipt::getMeetingId, meetingId).eq(MeetingReceipt::getType, 0))
                .toList().stream().filter(p -> Objects.equals(p.getState(), 1l)).collect(Collectors.toList());
        var meetingInfo = queryHelper.queryable(MeetingInfo.class).whereById(meetingId).firstOrNull();
        var usersStr = meetingInfo.getUsers();
        var userIds = JSONUtil.toList(usersStr, Long.class);
        var signIds= re.stream().map(p->p.getUserId()).collect(Collectors.toList());
        var noSignIds= userIds.stream().filter(p->!signIds.contains(p)).collect(Collectors.toList());
       var noSignUser= queryHelper.queryable(UserMemberDeptV.class)
               .where(p->p.in(UserMemberDeptV::getUserId,noSignIds))
               .toList().stream().map(p->{
                   MeetingReceipt mrTmp=new MeetingReceipt();
                   DataUtil.copyProperties(p,mrTmp);
                   mrTmp.setMeetingId(meetingId);
                   mrTmp.setState(0l);
                   mrTmp.setType(0l);
                   mrTmp.setUserName(p.getNickName());
                   return mrTmp;
               }).collect(Collectors.toList());
        return R.ok(Tuples.of(userIds.size(), re,noSignUser));
    }
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('partybuilding:MeetingReceipt:add')")
    @Log(title = "会议回执", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public R<Long> add(@RequestBody MeetingReceipt entity){
        return super.add(entity);
    }
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('partybuilding:MeetingReceipt:remove')")
    @Log(title = "会议回执", businessType = BusinessType.DELETE)
    @PostMapping("remove/{ids}")
    public R<Long> remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    @ApiOperation("编辑")
    @PreAuthorize("@ss.hasPermi('partybuilding:MeetingReceipt:edit')")
    @Log(title = "会议回执", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public  R<Long> edit(@RequestBody MeetingReceipt entity){
        return super.edit(entity);
    }
    @ApiOperation("导出")
    @PreAuthorize("@ss.hasPermi('partybuilding:MeetingReceipt:export')")
    @Log(title = "会议回执", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MeetingReceipt entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }


}